using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;  //  for Vector2
using Microsoft.Xna.Framework.Graphics;   //   for Texture2D

namespace zombieArena
{
    class Animation
    {
        Vector2 position;
        Texture2D texture;
        public int textureReference;

        // source rectangle stuff
        int x;
        int y;
        int width;
        int height;

        // sprite counter
        public int currentSprite;
        int maxSprites;

        // time the animation will remain on the screen
        public float lifeTime;
        public float maxLifeTime;

        // used to change between sprites
        float interval;
        float intervalTimer;

        public float scale = 1f;
        public float rotation = 0f;

        // if true, animation will loop
        public bool repeat;
        // if true, animation will fade over lifetime
        public bool fading;
        // if true, life time will not drop
        public bool infinite;

        // next frame if required
        public Animation nextFrame;

        // optimization stuff
        Vector2 screenPosition;
        Rectangle sourceRect;
        Color color;

        public Animation(Vector2 position, Texture2D texture, int x, int y, int width, int height, int maxSprites, float maxLifeTime, bool repeat, bool fading)
        {
            this.position = position;
            this.textureReference = -1;
            this.texture = texture;
            this.lifeTime = 0;
            this.maxLifeTime = maxLifeTime;
            this.repeat = repeat;
            this.fading = fading;
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.currentSprite = 0;
            this.maxSprites = maxSprites;
            this.interval = 100f;
            this.color = Color.White;
            this.infinite = false;
            this.nextFrame = null;

            this.screenPosition = position;
            this.sourceRect = new Rectangle(x,y,width,height);
        }

        public Animation(Vector2 position, int textureReference, int x, int y, int width, int height, int maxSprites, float maxLifeTime, bool repeat, bool fading)
        {
            this.position = position;
            this.textureReference = textureReference;
            this.lifeTime = 0;
            this.maxLifeTime = maxLifeTime;
            this.repeat = repeat;
            this.fading = fading;
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.currentSprite = 0;
            this.maxSprites = maxSprites;
            this.interval = 100f;
            this.color = Color.White;
            this.infinite = false;
            this.nextFrame = null;

            this.screenPosition = position;
            this.sourceRect = new Rectangle(x, y, width, height);
        }

        public void update(ref GameTime gameTime)
        {
            if (!infinite)
            {
                lifeTime += gameTime.ElapsedGameTime.Milliseconds;
            }
            intervalTimer += gameTime.ElapsedGameTime.Milliseconds;

            if (intervalTimer > interval && currentSprite < maxSprites)
            {
                currentSprite++;
                if (currentSprite == maxSprites && repeat)
                    currentSprite = 0;
                else if(currentSprite == maxSprites && !repeat)
                    currentSprite = maxSprites - 1;
                intervalTimer = 0;
            }
            if (fading)
            {
                byte fadeColor = (byte)(255 * ((maxLifeTime - lifeTime) / maxLifeTime));
                color.A = fadeColor;
                color.R = fadeColor;
                color.B = fadeColor;
                color.G = fadeColor;

            }
        }

        public void draw(ref SpriteBatch spriteBatch, ref Rectangle viewPort)
        {
            screenPosition.X = position.X - viewPort.X;
            screenPosition.Y = position.Y - viewPort.Y;

            sourceRect.X = (width * currentSprite) + x;

            spriteBatch.Draw(texture, screenPosition, sourceRect, color, rotation, new Vector2(width / 2, height / 2), scale, SpriteEffects.None, 0f);
        }

        public void draw(ref SpriteBatch spriteBatch, ref Rectangle viewPort, ref Texture2D textureRef)
        {
            screenPosition.X = position.X - viewPort.X;
            screenPosition.Y = position.Y - viewPort.Y;

            sourceRect.X = (width * currentSprite) + x;

            spriteBatch.Draw(textureRef, screenPosition, sourceRect, color, rotation, new Vector2(width / 2, height / 2), scale, SpriteEffects.None, 0f);
        }
    }
}
